import os
import sys
import time

import parsel
import requests

from src import facade
from src.facade import configdicts

sys.path.append("..")
from src.mylogger import MLogger
from src.mysqlfacade import MysqlUtiles

abspath = os.path.dirname(os.path.abspath(__file__))
configpath = os.path.join(abspath, "db.ini")


class DownYearsAndPara(object):
    """
    下载年并解析期
    """

    def __init__(self):
        self.logger = MLogger().streamlogger
        self.mysqlutile = MysqlUtiles(configpath, "db", logger=self.logger)
        self.host = configdicts["other"]["host"]
        self.baseurl = "http://" + self.host + "/Kns55/oldnavi/"
        self.header = {
            "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3",
            "Accept-Encoding": "gzip, deflate",
            "Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8",
            "Host": self.host,
            "Upgrade-Insecure-Requests": "1",
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36"
        }
        self.sn = requests.Session()

    def selectdb(self):
        sql = "select url,baseid,`year` from `year` where stat=0 and failcount < 3 limit 1000"
        rows = self.mysqlutile.SelectFromDB(sql)
        print("**")
        if rows:
            for row in rows:
                fullurl = self.baseurl + row[0]
                BoolResult, errString, r = facade.BaseRequest(fullurl,
                                                              sn=self.sn,
                                                              headers=self.header,
                                                              mark="colbissue",
                                                              timeout=(30, 60))
                if BoolResult:
                    select = parsel.Selector(text=r.text)
                    li_list = select.xpath('//*[@id="lblIssue"]/ul/li')
                    Listsql = []
                    for li_tag in li_list:
                        url = li_tag.xpath('.//a/@href').get()
                        issue = li_tag.xpath('.//a/text()').get()
                        print(issue)
                        issue = issue.split("年")[-1].replace("期", "")

                        sql = "insert ignore into issue (url,baseid,`year`,issue) values ('%s','%s','%s','%s')"
                        sql = sql % (url, row[1], row[2], issue)
                        Listsql.append(sql)
                    self.mysqlutile.ExeSqlListToDB(Listsql)
                    sql = "update `year` set stat=1 where baseid='{}' and `year`='{}'".format(row[1], row[2])
                    self.mysqlutile.ExeSqlToDB(sql)
                else:
                    sql = "update `year` set failcount=failcount+1 where baseid='{}' and `year`='{}'".format(row[1],
                                                                                                             row[2])
                    self.mysqlutile.ExeSqlToDB(sql)
                    sql = "select url from journal where baseid = '{}'".format(row[1])
                    rows = self.mysqlutile.SelectFromDB(sql)
                    url = rows[0][0]
                    BoolResult, errString, r = facade.BaseRequest(fullurl,
                                                                  sn=self.sn,
                                                                  headers=self.header,
                                                                  mark="tdInfo",
                                                                  timeout=(30, 60))
                    if BoolResult:
                        self.logger.info("请求期刊页成功")
                    else:
                        self.logger.info("请求期刊页失败")

                    print(r.text)
        else:
            return "break"


"""
alert('检索服务器暂时没有提供服务,请您重新登录到其它服务器！');
"""
if __name__ == "__main__":
    down = DownYearsAndPara()
    while True:
        results = down.selectdb()
        if results == "break":
            break
